<template lang="pug">
.list
  .header
    .header-search
      el-input(placeholder="需求名称/发布者" prefix-icon="el-icon-search" v-model="searchString" @change="changeSearch")
      el-select(placeholder="选择所属行业" prefix-icon="el-icon-search" clearable v-model="styleId" @change="changeBrand")
        el-option(v-for="item in styleList", :label="item.styleName", :value="item.styleId", :key="item.styleId")
      el-select(placeholder="一级分类" prefix-icon="el-icon-search" clearable v-model="categoryId" @change="changeCategory")
        el-option(v-for="item in cateList", :label="item.title", :value="item.id", :key="item.id")
    .header-btn
      el-button(type="primary", :disabled="isDisabled" size="small" @click="batchAgree" v-if="$permission('AB-04003-001')") 批量通过
      el-button(name="info", :disabled="isDisabled" size="small" @click="batchRefuse" v-if="$permission('AB-04003-001')") 批量拒绝
  .list-table
    el-table(
      height="100%"
      :data="list"
      :header-cell-style="{ fontSize: '14px', color: '#64698F', background: '#F8F7FC' }"
      border
      style="width: 100%"
      v-loading="loading"
      empty-text="未找到符合条件的内容"
      @selection-change="handleSelectionChange"
    )
      el-table-column(type="selection" width="55" align="center")
      el-table-column(type="index" width="55" align="center" label="序号")
      el-table-column( label="需求名称" min-width="100")
        template(slot-scope="scope")
          .table-item-bd.flex.click(@click="seeDetail(scope.row)")
            .table-item-name {{scope.row.demandName}}
      el-table-column( label="发布者" prop="userName")
      el-table-column( label="业务范围" prop="style" min-width="120")
        template(slot-scope="scope")
          .table-item-bd.flex
            .tag-item(v-for="item in scope.row.style") {{item.styleName}}
      el-table-column( label="需求分类" prop="categoryName")
      el-table-column( label="找材状态" prop="isFinish")
        template(slot-scope="scope") {{scope.row.isFinish ? '完成' : '未完成'}}
      el-table-column( label="发布时间" prop="createTime" sortable)
        template(slot-scope="scope")
          .table-item-bd {{scope.row.createTime | filterDate}}
      el-table-column( label="操作")
        template(slot-scope="scope")
          .table-item-bd
            .table-item-handle
              .table-item-btn.warning(@click="agree(scope.row)" v-if="$permission('AB-04003-001')") 通过
              .table-item-btn.info(@click="refuse(scope.row)" v-if="$permission('AB-04003-001')") 拒绝
  .table-pagination
    el-pagination(
      background
      layout="prev, pager, next, jumper"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
      @current-change="currentChange"
    )
</template>

<script>
import { getMaterialFindList, getCategoryList, getStyleList, auditFindMaterials } from 'api'
export default {
  name: 'materialAuditList',
  data () {
    return {
      page: 1,
      pageSize: 20,
      total: 0,
      searchString: '',
      styleId: '',
      categoryId: '',
      list: [],
      loading: false,
      cateList: [],
      styleList: [],
      selectionList: []
    }
  },
  computed: {
    isDisabled () {
      return !this.selectionList.length
    }
  },
  components: {},
  methods: {
    changeSearch () {
      this.page = 1
      this.getList()
    },
    changeBrand () {
      this.page = 1
      this.getList()
    },
    changeCategory () {
      this.page = 1
      this.getList()
    },
    handleSelectionChange (rows) {
      this.selectionList = rows
    },
    seeDetail (row) {
      this.$router.push({ name: 'findMaterialDetail', params: { id: row.id } })
    },
    batchAgree () {
      this.$confirm('是否审核通过所选材料？', '批量审核通过', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success'
      }).then(async () => {
        try {
          let idList = this.selectionList.map(item => item.id).join(',')
          await this.$axios.post(auditFindMaterials, { idList: idList, status: 2 })
          this.$message.success('审核成功！')
          this.getList()
        } catch (err) {
          console.log(err)
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    agree (row) {
      this.$confirm('是否审核通过该材料？', '审核通过', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success'
      }).then(async () => {
        try {
          console.log(row)
          await this.$axios.post(auditFindMaterials, { idList: row.id + '', status: 2 })
          this.$message.success('审核成功！')
          this.getList()
        } catch (err) {
          console.log(err)
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    refuse (row) {
      this.$confirm('是否拒绝所选材料？', '审核失败', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        try {
          await this.$axios.post(auditFindMaterials, { idList: row.id + '', status: 3 })
          this.$message.success('审核成功！')
          this.getList()
        } catch (err) {
          console.log(err)
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    batchRefuse () {
      this.$confirm('是否拒绝所选材料？', '批量审核失败', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        try {
          let idList = this.selectionList.map(item => item.id).join(',')
          await this.$axios.post(auditFindMaterials, { idList: idList, status: 3 })
          this.$message.success('审核成功！')
          this.getList()
        } catch (err) {
          console.log(err)
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    currentChange (page) {
      this.page = page
      this.getList()
    },
    async getList () {
      try {
        this.loading = true
        let query = { ...this.$route.query, reviewStatus: 1 }
        query.currentPage = this.page
        query.pageLines = this.pageSize
        if (this.searchString) query.searchString = this.searchString
        if (this.styleId) query.styleId = this.styleId
        if (this.categoryId) query.categoryId = this.categoryId
        const { ResultSet } = await this.$axios.post(getMaterialFindList, { ...query })
        this.total = ResultSet.recordNumber
        this.list = ResultSet.userData.findList
        this.loading = false
      } catch (err) {
        this.loading = false
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getCategoryList () {
      try {
        const { ResultSet } = await this.$axios.post(getCategoryList)
        this.cateList = ResultSet.userData.result
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getStyleList () {
      try {
        const { ResultSet } = await this.$axios.post(getStyleList)
        this.styleList = ResultSet.userData.styleList
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async init () {
      await this.getList()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getCategoryList()
      vm.getStyleList()
      vm.init()
    })
  },
  beforeRouteUpdate (to, from, next) {
    next()
    this.init()
  }
} 
</script>

<style lang="scss" scoped>
.list {
  width: 100%;
  height: 100%;
  background: #FFFFFF;
  border: 1px solid #F3F3F4;
  box-sizing: border-box;
  box-shadow: 0px 4px 0px #F3F3F4;
  border-radius: 10px;
  padding: 0 40px;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  .el-input {
    width: 202px;
    margin-right: 16px;
  }
  .el-select {
    margin-right: 16px;
  }
  .header-search {
    display: flex;
    align-items: center;
  }
  .header-info {
    font-size: 14px;
    line-height: 22px;
    color: #8B8EAB;
  }
}
.list-table {
  height: calc(100% - 150px);
}
.table-pagination {
  padding: 24px 0;
  text-align: right;
}
.table-item-bd {
  &.flex {
    display: flex;
    align-items: center;
  }
  &.click {
    cursor: pointer;
  }
  .tag-item {
    padding: 1px 10px;
    background: rgba(134, 118, 255, 0.1);
    border-radius: 6px;
    font-size: 14px;
    line-height: 22px;
    color: #64698F;
    margin-right: 4px;
    margin-bottom: 4px;
  }
  .table-item-head {
    width: 24px;
    height: 24px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    border-radius: 4px;
    margin-right: 8px;
  }
  .table-item-name {
    font-size: 14px;
    line-height: 22px;
    color: #FF2E42;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .table-item-sales {
    display: flex;
    align-items: center;
  }
  .item-sales {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 5px 8px;
    border: 1px solid #F7DCB7;
    box-sizing: border-box;
    border-radius: 2px;
    &.success {
      border-color: #C0E5D1;
      background: #F2FAF6;
      color: #32C37D;
    }
    &.warning {
      border-color: #F7DCB7;
      background: #FFF7EC;
      color: #FC9700;
    }
    &.info {
      border-color: #B2B4C7;
      background: #F6F6F9;
      color: #8B8EAB;
    }
    span {
      display: inline-block;
      font-size: 14px;
      line-height: 22px;
    }
    .iconfont {
      margin-left: 6px;
      font-size: 16px;
      color: #CCD2E3;
    }
  }
}
.table-item-status {
  display: flex;
  align-items: center;
  .status-point {
    position: relative;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 4px;
    &:before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      width: 4px;
      height: 4px;
      transform: translate(-50%, -50%);
    }
    &.success {
      background: rgba(48, 217, 135, .5);
      &:before {
        background: #30D987;
      }
    }
    &.warning {
      background: rgba(251, 177, 66, .5);
      &:before {
        background: #FBB142;
      }
    }
    &.normal {
      background: rgba(134, 118, 255, .5);
      &:before {
        background: #8676FF;
      }
    }
  }
  .iconfont {
    font-size: 16px;
    margin-right: 8px;
  }
  .icon-normal {
    color: #32C37D;
  }
  .icon-block-up {
    color: #FBB142;
  }
}
.table-item-handle {
  display: flex;
  .table-item-btn {
    margin-right: 8px;
    padding: 5px 8px;
    font-size: 12px;
    line-height: 20px;
    border-radius: 6px;
    cursor: pointer;
    &.warning {
      background: #FDEFF1;
      color: #FF2E42;
    }
    &.normal {
      background: #F3F0FF;
      color: #7362F0;
    }
    &.info {
      background: #F6F6F9;
      color: #3E4373;
    }
  }
}
</style>
